package com.health.gateway.filter;

import com.health.core.constant.CommonConstants;
import com.health.core.util.GeneralUtil;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

// 需要校验token的过滤器

/**
 * @Author tidudada
 * @Description 目前的需求是服务访问需要校验jwt令牌
 * @Date 上午10:09 2022/1/13
 **/
// TODO 解析jwt
@Configuration
public class AccessGatewayFilter implements GlobalFilter  {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain gatewayFilterChain) {
        ServerHttpRequest request = exchange.getRequest();
        PathContainer pathContainer = request.getPath().pathWithinApplication();
        // 添加gatewayKey，防止下游接口直接被访问
        ServerHttpRequest.Builder mutate = request.mutate();
        String uuid = GeneralUtil.getUUID();
        mutate.header("gatewayKey",uuid);
        mutate.header("keyEncrypt",GeneralUtil.encrypt(CommonConstants.ENCRYPTED,uuid));
        return gatewayFilterChain.filter(exchange.mutate().request(mutate.build()).build());
    }

}
